package edu.zut.cs.software.perfect.student.web.spring.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.perfect.base.web.spring.controller.GenericController;
import edu.zut.cs.software.perfect.student.domain.Student;
import edu.zut.cs.software.perfect.student.service.StudentManager;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author liangying
 * @Date 2020年5月30日 上午12:24:46
 * @Version 1.0
 */
@RestController
@RequestMapping("/classes/student")
public class StudentController extends GenericController<Student, Long, StudentManager>{

	StudentManager studentManager;

	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
	/*public String index() {
		String result = "/student/index";
		return result;
	}*/
	public String getStudentAll() {
		return JSON.toJSONString(studentManager.findAll());
	}

	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
		this.manager = this.studentManager;
	}
	@ResponseBody
    @GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAllStudent() {
		List<Student> studentList = this.manager.findAll();
        PageInfo pageInfo = new PageInfo(studentList);
        return JSON.toJSONString(pageInfo);
	}
	
	@GetMapping("/findbyid/{id}")
	public String findById(@PathVariable("id") Long id) {
		Optional<Student> result = this.manager.findByCourseId(id);
		return JSON.toJSONString(result);
	}
	
//	@DeleteMapping("/deletebyid/{id}")
	@GetMapping("/deletebyid/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
//		Optional<CourseInformation> courseInformation = this.manager.findByCourseId(id);
		Boolean result = this.manager.deleteStudentId(id);
		return result;
	}
	
}
